#include <stdlib.h>
#include "bellman.h"

extern void EM_reset(char*);
extern void destroy(A_exp);

int main(int argc, char **argv)
{
    FILE *outfile;
    T_stmList list;
    T_stmList listNew, listOld = NULL, listResult = NULL;
    struct C_block block;
    T_stmList ptr;
    AS_instrList instr;

    EM_reset("test.bm");
    if (!yyin)
    {
        fprintf(stderr, "Can't open input file!\n");
        exit(1);
    }

    yyparse();
    if (!absyn_root)
        exit(1);

    list = SEM_transProg(absyn_root);
    outfile = fopen("treeRaw.out","w");
    printStmList(outfile, list);
    fclose(outfile);

    ptr = list;
    while(ptr)
    {
        listNew = C_linearize(ptr->head);
        if (!listOld)
        {
            listResult = listOld = listNew;
        }
        else
        {
            listOld->tail = listNew;
            listOld = listNew;
        }
        ptr = ptr->tail;
    }
        
    block = C_basicBlocks(listResult);
    listResult = C_traceSchedule(block);
    outfile = fopen("treeRegular.out","w");
    printStmList(outfile, listResult);
    fclose(outfile);

    // generate presue assembly code
    instr = F_codegen(Tr_outermost()->frame, listResult);
    outfile = fopen("out.asm","w");
    AS_printInstrList(outfile, instr, Temp_name());
    fclose(outfile);

    exit(0);
}
